var shareApi = 'https://api.igoyp.com/'
var mealApi = 'https://api.meal.52g3.cc/'

module.exports = {
   getMealApi: getMealApi,
   getShareApi: getShareApi,
   toast: toast,
   modal: modal,
   empty: empty,
   isArray: isArray,
   addValueForKey: addValueForKey,
   getIndexInList: getIndexInList,
   twoObjIsSame: twoObjIsSame,
   getLoc: getLoc,
   shareApi: shareApi,
   getNowFormatDate: getNowFormatDate,
   confirm: confirm,
   Toast: Toast,
   Modal: Modal
}
// 获取餐饮接口
function getMealApi(urlTail, param, callback) {
   var aipUrl = mealApi + urlTail
   request(aipUrl, param, callback)
}
// 获取共享接口
function getShareApi(urlTail, param, callback) {
   var aipUrl = shareApi + urlTail
   request(aipUrl, param, callback)
}
// 获取点餐系统服务器接口
function getMealApi(urlTail, param, callback) {
   var aipUrl = mealApi + urlTail
   request(aipUrl, param, callback)
}
// 调用微信获取数据接口
function request(aipUrl, param, callback) {
   wx.request({
      url: aipUrl,
      data: param,
      method: 'POST',
      success: function (res) {
         console.log('【地址】 ' + aipUrl, '\n【参数】', param, '\n【成功】', res);
         var d = res.data
         if (d == '') return
         if (typeof (callback) == 'function') {
            callback(d)
         }
      },
      fail: function (res) {
         toast('网络请求失败！')
         console.log('【地址】 ' + aipUrl, '\n【参数】', param, '\n【失败】', res);
      },
   })
}

// 提示
function Toast(msg) {
   wx.showToast({
      title: msg
   })
}
// 带图标提示
function toast(msg) {
   wx.showToast({
      title: msg,
      image: '/resource/img/huaji.png',
   })
}
//不带标题提示
function Modal(text, confirmText = 'OK') {
   wx.showModal({
      content: text,
      cancelText: '',
      confirmText: confirmText,
   })
}
//带标题提示
function modal(msg, title) {
   wx.showModal({
      title: title ? title : '温馨提示',
      content: msg,
   })
}
// 删除提醒
function confirm(text, cancelText, confirmText, callback) {
   wx.showModal({
      content: text,
      showCancel: true,
      cancelColor: '',
      cancelText: cancelText,
      confirmText: confirmText,
      confirmColor: '',
      success: function (res) {
         console.log('confirm:success:', res)
         if (callback) callback(res.confirm)
      },
      fail: function (res) { },
   })
}
// 判断是否为空
function empty(val) {
   if (val == undefined || val == null || val == '' || val == 'undefined') {
      return true;
   }
   if (isArray(val)) {
      if (val.length == 0) {
         return true;
      }
   }
   return false;
}
//增加key和value
function addValueForKey(toDict, fromDict, keyArray = []) {
   if (!isArray(keyArray)) {
      toast('第3个传参类型错误')
      return toDict
   }
   var len = keyArray.length
   if (len == 0) {
      for (var key in fromDict) {
         toDict[key] = fromDict[key]
      }
   } else {
      for (var i = 0; i < len; i++) {
         key = keyArray[i]
         if (fromDict[key]) {
            console.log('有key[' + i + ']==' + key)
            toDict[key] = fromDict[key]
         } else {
            console.log('没key[' + i + ']==' + key)
         }
      }
   }
   return toDict
}
//查对象在对象数组中的下标，不存在则返回-1，keyArray是要求比对的key
function getIndexInList(obj, list, keyArray = []) {
   var count = list.length
   if (count == 0 || !isArray(list) || empty(list))
      return -1
   for (var i = 0; i < count; i++) {
      if (twoObjIsSame(obj, list[i], keyArray)) {
         return i
      }
   }
   return -1
}
//判断两对象是否相同
function twoObjIsSame(obj, _obj, keyArray = []) {
   var len = keyArray.length
   if (len == 0) {
      for (var key in obj) {
         if (obj[key] != _obj[key]) {
            console.log('obj.' + key + '==' + obj[key] + ',_obj.' + key + '== ' + _obj[key])
            return 0
         }
      }
   } else {
      for (var i = 0; i < len; i++) {
         key = keyArray[i]
         if (obj[key] != _obj[key]) {
            console.log('obj.' + key + '==' + obj[key] + ',_obj.' + key + '== ' + _obj[key])
            return 0
         }
      }
   }
   return 1
}
//获取数据类型
function getType(array) {
   return Object.prototype.toString.call(array)
}
//是否数组
function isArray(obj) {
   return getType(obj) == '[object Array]'
}
//从对象获取经纬度
function getLoc(obj) {
   return {
      latitude: obj.latitude,
      longitude: obj.longitude
   }
}
function getNowFormatDate(type) {
   //type==1,只要日期 如果type==2 要日期时间
   var date = new Date();
   var seperator1 = "-";
   var seperator2 = ":";
   var month = date.getMonth() + 1;
   var strDate = date.getDate();
   if (month >= 1 && month <= 9) {
      month = "0" + month;
   }
   if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
   }

   var currentDate = date.getFullYear() + seperator1 + month + seperator1 + strDate

   if (type == 2) {
      currentDate += " " + date.getHours() + seperator2 + date.getMinutes()
         + seperator2 + date.getSeconds();
   }

   return currentDate;
}
//顾客点餐后通知生成小票
function setNoticeTicket(noti) {
   this.ticketNotice = noti
}
function noticeTicket(noti) {
   if (this.ticketNotice)
      this.ticketNotice(noti)
}